<?php

namespace app\controller;

use app\BaseController;
use DOMDocument;
use DOMXPath;

class DataMining extends BaseController
{
    public function index()
    {
        $url = 'https://ssr1.scrape.center/';
        $data = $this->get($url);
        // 匹配电影所在位置
        $list_preg = '/<li class="yk-col4 mr1">.+<\/li>/Us';
        // 匹配img标签上的src和alt
        $img_preg = '/<img class="quic" _src="(.*)" src="(.*)" alt="(.*)" \/>/U';
        //匹配电影的url
        $video_preg = '/<a href="(.*)" title="(.*)" target="(.*)"><\/a>/U';
        //把所有的li存到$list里，$list是个二维数组
        preg_match_all($list_preg, $data, $list);

        foreach ($list[0] as $k => $v) {   //这里$v就是每一个li标签
            //获取图片及电影名称
            /*preg_match($img_preg,$v,$img);  //把匹配到的图片的信息存到$img里
            var_dump($img);*/

            //获取电影地址
            /* preg_match($video_preg,$v,$video);  //把匹配到的电影的信息存到$video里
              var_dump($video);*/


            preg_match($img_preg, $v, $img);
            preg_match($video_preg, $v, $video);
            echo $img[0] . '<a href="' . $video[1] . '">' . $video[2] . '</a>';
        }
    }


    public function get($url)
    {
        $curl = curl_init();
        // 配置curl中的http协议->可配置的荐可以查PHP手册中的curl_
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_HEADER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        // 执行这个请求
        return curl_exec($curl);
    }

    public function tzurl()
    {
        $url = 'https://ssr1.scrape.center/';
        // 创建上下文
//        $context = stream_context_create([
//            'http' => [
//                'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36\r\n"
//            ]
//        ]);

        // 使用上下文发送请求
//        $response = file_get_contents($url, false, $context);
        $response = file_get_contents($url);
        var_dump($response);
        // 检查是否成功获取到数据
        if ($response === false) {
            // 处理获取数据失败的情况
            $error = 'Failed to get data';
            // 输出错误信息
            echo json_encode(['error' => $error]);
        } else {
            var_dump($response);
            $data = json_decode($response, true);

            // 检查是否成功解析JSON数据
            if ($data === null) {
                // 处理解析JSON失败的情况
                $error = 'Failed to parse JSON data';
                // 输出错误信息
                echo json_encode(['error' => $error]);
            } else {
                // 提取数据部分
                $result = $data['data'];

                // 将数据以JSON形式输出
                echo json_encode($result);
            }
        }
    }

    public function urlDetail()
    {
        for ($i = 93; $i <= 93; $i++) {
            $url = 'https://ssr1.scrape.center/detail/' . $i;
            $html = file_get_contents($url);
            $dom = new DOMDocument;
            @$dom->loadHTML($html); // 使用@抑制加载HTML中的警告
            $xpath = new DOMXPath($dom);
// 使用XPath查询提取数据，例如提取所有的链接
            $list = $xpath->query("//div[@class='item el-row']//h2[@class='m-b-sm']|//div[@class='item el-row']//img[@class='cover']|//button[@class='el-button category el-button--primary el-button--mini']|//div[@class='m-v-sm info']//span");
//            $name = $xpath->query("//div[@class='item el-row']//h2[@class='m-b-sm']");
//            $image = $xpath->query("//div[@class='item el-row']//img[@class='cover']");
//            $type = $xpath->query("//button[@class='el-button category el-button--primary el-button--mini']");
//            $date = $xpath->query("//div[@class='m-v-sm info']//span");
            $introduce = $xpath->query("//div[@class='drama']//p");
            foreach ($introduce as $v){
                $data[$i]['introduce'] = $v->nodeValue;
            }
            $score = $xpath->query("//p[@class='score m-t-md m-b-n-sm']");
            foreach ($score as $v){
                $data[$i]['score'] = $v->nodeValue;
            }
            $director = $xpath->query("//div[@class='directors el-row']//p");
            $data[$i]['director'] = '';
            foreach ($director as $v){
                var_dump($v->nodeValue);
                $data[$i]['director'] .= $v->nodeValue . ",";
            }
            foreach ($list as $link) {
                switch ($link->nodeName) {
                    case 'h2':
                        $result = $link->nodeValue;
                        $parts = explode(' - ', $result);
                        if (count($parts) == 2) {
                            // 使用 trim 去除前后空格
                            $chineseName = trim($parts[0]);
                            $englishName = trim($parts[1]);
                            $data[$i]['name_cn'] = $chineseName;
                            $data[$i]['name'] = $englishName;
                        }
                        break;
                    case 'button':
                        foreach ($director as $v){
                            var_dump($v->nodeValue);
                            $data[$i]['director'] .= $v->nodeValue . ",";
                        }
                        break;
                    case 'span':
                        $result = $link->nodeValue;
                        if (preg_match('/\d{4}-\d{2}-\d{2}/', $result, $matches)) {
                            $data[$i]['date'] = $matches[0];
                        }
                        break;
                    case 'img':
                        $data[$i]['image'] = $link->getAttribute('src');
                        break;

                }
            }
        }

//        for ($i = 1; $i <= 100; $i++) {
//            $url = 'https://ssr1.scrape.center/detail/'.$i;
//            $list[$i-1] = file_get_contents($url);
//        }
//        var_dump($list[0]);
    }

    public function film(){

    }

}

